<?php
/**
 * 在线订单功能类文件
 * @author		Arthur(ArthurXF@gmail.com)
 * @copyright	(c) 2006 by bizeway.com
 * @version		$Id$
 * @package		ArthurXF
 * @subpackage	deal
 */
error_reporting(0);
class deal extends ArthurXF{
	public $thisModel = 'deal';
	public $tablename2 = 'deal';
	public $tablename3 = 'user';

	/**
	 * 保存信息内容
	 * @author	肖飞
	 * @param	int $arrData    信息信息数组
	 * @return  void
	 */
	function saveInfo($arrData,$intModify=0){
		$arr = array();
		$arr = check::SqlInjection($this->saveTableFieldG($arrData));

		if($intModify == 0){
			//if(!empty($_SESSION['user_id'])) $arr['user_id'] = intval($_SESSION['user_id']);
			if($this->insertInfo($arr)){
				check::Alert("下订单成功",0);
			}else{
				check::Alert("下订单失败",0);
			}
		}
	}


	 /**
	 * 取得会员详细信息
	 * @author	肖飞
	 * @param	int $intUserID    会员id
	 * @return  array
	 */

	function getUser($intUserID){
		$strSQL = "SELECT * FROM $this->tablename3 WHERE user_id =".$intUserID;		
		//$rs = $this->db->prepare($strSQL);
		//$rs->execute(array($intUserID));
		$rs = $this->db->query($strSQL);		
		$arrData = $rs->fetchall();			
		//$arrData = $this->loadTableFieldG($arrData);
		

		return $arrData;
	}


	/**
	 * 取得数据记录
	 * @author	肖飞
	 * @param	string	$table		表名
	 * @param	array	$arrData    数据记录信息数组
	 * @param	bool	$blComplex	使用优化查询,超大数据量效果明显,小数据不建议使用
	 * @version	2007-11-21
	 * @return  Boolean
	 */

	//$limit = ($intPage-1)*$arrGPage['page_size'],$arrGPage['page_size'];
	function selectDataG($table,$where = '',$limit,$field = '*',$blFetch = false,$arrData = array(),$blCount = false,$blComplex = false ){
			//$strSQL = "SELECT SQL_CALC_FOUND_ROWS $field from $table $where";		效率低下,在MYSQL版本未改进之前弃用
			$strSQL = "SELECT $field from $this->tablename2 $where $limit";
					
			if($this->arrGPdoDB['PDO_CACHE']) {
				$strCacheName = md5($strSQL);
				$strCacheDir = '';
				for($i=0;$i<32;$i+=2){
					$strCacheDir .= '/'.$strCacheName[$i].$strCacheName[$i+1];
				}

				$strCacheName = $strCacheDir.'SQL_'.$this->tablename2.'_'.$strCacheName;
				$strCacheFile = $this->arrGPdoDB['PDO_CACHE_ROOT'].'/'.$strCacheName;
				$objCache = new cache($strCacheFile,$this->arrGPdoDB['PDO_CACHE_LIFETIME']);
				if($this->arrGPdoDB['PDO_CACHE']==1) {
					if($objCache->cache_is_active()) {
						include($strCacheFile);
						if($arr['COUNT_ROWS'] != '' ) $_SESSION['COUNT_ROWS'] = $arr['COUNT_ROWS'];
						else $arr['COUNT_ROWS'] = $_SESSION['COUNT_ROWS'];
						return $arr;
					}
				}
			}
			if($this->arrGPdoDB['PDO_DEBUG']) echo $strSQL.'<br><br>';
			$rs = $this->db->prepare($strSQL);
			$rs->execute($arrData);
			if($blFetch) $arr = $rs->fetch();
			else  $arr = $rs->fetchAll();
			$rs = '';
			if($blCount){
					//$strSQL = "SELECT FOUND_ROWS()";		配合SQL_CALC_FOUND_ROWS使用的
					//$strSQL = "SELECT count(DISTINCT id) from $table $where";
					if(!$blComplex){
						$strSQL = "SELECT count(*) as num from $this->tablename2 $where";
						if($this->arrGPdoDB['PDO_DEBUG']) echo $strSQL.'<br><br>';
						$rs = $this->db->query($strSQL);
						if(strpos($where,'GROUP') || strpos($where,'group')){
							$arrTemp = $rs->fetchAll();
							$arr['COUNT_ROWS'] = count($arrTemp);
						}else{
							$arrTemp = $rs->fetch();
							$arr['COUNT_ROWS'] = $arrTemp['num'];
						}
					}
					if($arr['COUNT_ROWS'] != '' ) $_SESSION['COUNT_ROWS'] = $arr['COUNT_ROWS'];
					else $arr['COUNT_ROWS'] = $_SESSION['COUNT_ROWS'];

			}
			if($this->arrGPdoDB['PDO_CACHE']){
				if(isset($objCache)&&is_object($objCache)) {
					$somecontent = '<?php' . "\n" . '$arr = ' . var_export( $arr, true ) . ';' . "\n" . '?>';
					$objCache->write_file($somecontent);
				}
			}
			return $arr;

	}

	/**
	 * 删除购买
	 * @author	肖飞
	 * @param	int $intDealID    购买id
	 * @return  void
	 */
	function deleteDeal($intDealID){
		$strSQL = "DELETE FROM $this->tablename2 WHERE `id` in $intDealID";
		return $this->db->exec($strSQL);
	}



	/**
	 * 成功购买
	 * @author	jesse
	 * @param	int $intDealID    购买id
	 * @param	int $intDealID    产品id
	 * @param	int $num		  购买数量
	 * @return  void
	 */
	function SuccessDeal($id){		
		$strSQL = "UPDATE $this->tablename2 SET `type_id` = 5  WHERE `id` in $id";
		return $this->db->exec($strSQL);
	}

	
	/**
	 * 取得购买内容
	 * @author	肖飞
	 * @param	int $id    购买ID
	 * @return  void
	 */
	function getDeal($id,$pass=null){
		if($pass!=null) $where= " and pass='$pass'";
		else $where="";
		$strSQL = "SELECT * FROM $this->tablename2 ".
		" Where id in ".$id.$where;		
		$rs = $this->db->query($strSQL);
		return $rs->fetch();
	}


	/**
	 * 修改订单
	 * @author	肖飞 ,郭小伟 
	 * @param	array $arrDeal    订单数组
	 * @return  void
	 */
	function updateDeal($arrDeal){	
		$strSQL = "	UPDATE $this->tablename2 SET ";
		foreach ($arrDeal as $k => $v) {
			if ($k == 'deal_no') continue ;
			$strSQL .= $k."='" . $v . "',";
		}
		$strSQL = substr($strSQL, 0, -1);
		$strSQL .= " WHERE deal_no='$arrDeal[deal_no]'";
		return $this->db->exec($strSQL);
	}

}
?>